﻿using DATAGRID实例MVVM.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DATAGRID实例MVVM.DB
{
    public class localDb
    {
        public localDb()
        {
            Init();
        }

        private List<Student> studentls;

        private void Init()
        {
            studentls = new List<Student>();
            for (int i = 0; i < 30; i++)
            {
                studentls.Add(new Student() { 
                    Id = i ,
                    Name= "Student"+i.ToString()
                }) ;
            }
        }

        public List<Student> GetStudents()
        {
            return studentls;
        }

        public void AddStudent(Student stu)
        {
            stu.Id = studentls.Max(s => s.Id) + 1;
            studentls.Add(stu);
        }

        public void DeleteStudentById(int id)
        {
           var s= studentls.FirstOrDefault(t => t.Id == id);
            if(s!=null)
            {
                studentls.Remove(s);
            }
        }

        public List<Student> GetStudentByName(string name)
        {
            return studentls.Where(q=>q.Name.Contains(name)).ToList();
        }

        public Student GetStudentById(int id)
        {
            var model= studentls.FirstOrDefault(s => s.Id==id);
            if(model!=null)
            {
                return new Student()
                {
                    Id = model.Id,
                    Name = model.Name
                };
            }
            return null;
        }

    }
}
